package com.mbc.web.common.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.web.common.shared.HttpStatus;
import com.mbc.common.entities.Currency;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.CommonMessage;
import com.mbc.web.common.client.ViewUtils;
import com.mbc.web.common.client.ui.control.GridLabelColumn;
import com.mbc.web.common.client.ui.control.GridView;
import com.mbc.web.common.client.ui.control.MButton;
import com.mbc.web.common.client.ui.control.Pager;
import com.mbc.web.common.shared.dto.CurrencyDataListDTO;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;

public class SearchCurrency extends AbstractListView<Currency> {

	@UiField GridView gvDisplay;
	@UiField Button btnSearch;
	@UiField Pager pager;
	@UiField TextBox txtKeyword;
	@UiField MButton btnAddNew;
	@UiField ListBox lstfield;

	public static final byte SAVE = 1;
	public static final byte DELETE = 0;
	
	private GridLabelColumn colCurrencyCode = new GridLabelColumn();
	private GridLabelColumn colDescription = new GridLabelColumn();
	private GridLabelColumn colSymbol = new GridLabelColumn();
	
	private static SearchCurrencyUiBinder uiBinder = GWT
			.create(SearchCurrencyUiBinder.class);

	interface SearchCurrencyUiBinder extends UiBinder<Widget, SearchCurrency> {
	}

	public SearchCurrency() {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
	}
	
	public SearchCurrency(String message, byte status){
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
		if(status == SAVE)
			ViewUtils.showSuccessMessage(message + " " + CommonMessage.saved_message);
		else if(status == DELETE)
			ViewUtils.showSuccessMessage(message + " " + CommonMessage.deleted_message);
	}
		
	public SearchCurrency(String title){
		initWidget(uiBinder.createAndBindUi(this));
		this.title = title;
		initialize();
	}

	@Override
	protected void initialize() {
		
		lstfield.addItem("Currency Code","CurrencyCode");
		lstfield.addItem("Description","Description");
		lstfield.addItem("Symbol","Symbol");
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			
			@Override
			public void execute() {
				txtKeyword.setFocus(true);
			}
		});
		colCurrencyCode.header = "Currency Code";
		colCurrencyCode.styleName = "clickable-col";
		colCurrencyCode.clickHandler = new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				//MBC Begin
				if(isModal){
					selectedEntity = dataList.get(gvDisplay.getSelectedRowIndex());
					closeModal();
				}
				else	// MBC End
					ClientSingleton.changeView(new FrmCurrency(dataList.get(gvDisplay.getSelectedRowIndex())));
			}
		};
		gvDisplay.addColumn(colCurrencyCode);
		
		colDescription.header = "Description";
		gvDisplay.addColumn(colDescription);
		
		colSymbol.header = "Symbol";
		gvDisplay.addColumn(colSymbol);
		
		pager.btnFirst_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		
		pager.btnPrevious_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		
		pager.btnNext_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		
		pager.btnLast_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		
		onBtnSearchClick(null);
	}

	@Override
	protected void bindDisplay(List<Currency> list) {
		gvDisplay.removeAllRows();
		for (Currency l_currency : list) {
			gvDisplay.addRow(new String[]{ l_currency.getCurrencyCode(), l_currency.getDescription(), l_currency.getSymbol()});
		}
		this.generateList();
	}

	@Override
	protected void search() {
		ViewUtils.startAjaxLoader(CommonMessage.loading_message);
		CommonControllerMgr.getCurrencyList(prepareCri(), getCurrencyListCallback());
	}
	
	private Currency prepareCri(){
		Currency l_cri = new Currency();
		l_cri.setKeyword(txtKeyword.getText());
		l_cri.setIndex(pager.index);
		l_cri.setSize(10);
		l_cri.setFieldname(lstfield.getValue(lstfield.getSelectedIndex()));
		return l_cri;
	}
	
	private AsyncCallback<CurrencyDataListDTO> getCurrencyListCallback(){
		return new AsyncCallback<CurrencyDataListDTO>() {
			
			@Override
			public void onSuccess(CurrencyDataListDTO result) {
				ViewUtils.stopAjaxLoader();
				if(result.getHttpStatus() == HttpStatus.OK){
					dataList = result.getCurrencyDataList().getEntityList();
					pager.totalRecord = result.getCurrencyDataList().getTotalRecord();
					bindDisplay(dataList);
					pager.prepareVisible();
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		};
	}
	@UiHandler("btnSearch")
	void onBtnSearchClick(ClickEvent event) {
		pager.index = 0;
		search();
	}
	
	@UiHandler("btnAddNew")
	void onBtnAddNewClick(ClickEvent event) {
		ClientSingleton.changeView(new FrmCurrency());
	}
	
	@UiHandler("txtKeyword")
	void onTxtKeywordKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER){
			onBtnSearchClick(null);
		}
	}
}
